<template>
  <div>
    <div class="selectaddress">
      <van-button type="info" @click="toAddressList" v-if="!flag">+选择收货地址</van-button>
      <div class="box" v-else @click="toAddressList">
        <div class="top">
          <span>收货人：{{addressList.name}}</span>
          <span>联系电话：{{addressList.tel}}</span>
        </div>
        <div
          class="bottom"
        >收货地址：{{addressList.province + addressList.city + addressList.county + addressList.addressDetail}}</div>
      </div>
    </div>
    <van-image width="100%" height="7" src="images/cart_border@2x.png" />
    <div
      v-for="(item,index) in goodsList"
      :key="item.goods_id"
      :class="{lastbox:index==goodsList.length-1?true:false}"
    >
      <van-card
        :num="item.goodsnum"
        :price="item.goods_price+'.00'"
        desc="描述信息"
        :title="item.goods_name"
        :thumb="item.pics[0].pics_sma"
      />
    </div>
    <div class="footer">登陆后下单</div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      goodsList: window.sessionStorage.getItem("toBuyGoodslist")
        ? JSON.parse(window.sessionStorage.getItem("toBuyGoodslist"))
        : [],
      addressList: {},
      flag: false
    };
  },
  methods: {
    toAddressList() {
      this.$router.push("/address_list");
    }
    // 将得到的数据保存在本地存储中防止刷新页面数据丢失
  },
  computed: {},
  created() {
    const address = JSON.parse(localStorage.getItem("address"));
    if (address) {
      this.flag = true;
      address.some(item => {
        if (item.isDefault === true) {
          this.addressList = item;
        }
        return;
      });
    } else {
      this.flag = false;
    }
  }
};
</script>

<style lang="less" scoped>
.selectaddress {
  position: relative;
  height: 85px;
}
.van-button {
  font-size: 12px;
  width: 120px;
  height: 30px;
  line-height: 30px;
  // margin: 0 auto !important;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.van-image {
  display: block;
}
.goods_list {
  margin-top: none !important;
}
.footer {
  text-align: center;
  position: fixed;
  height: 50px;
  line-height: 50px;
  width: 100%;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  background-color: #07c160;
  color: #fff;
}
.box {
  padding: 15px 10px;
  font-size: 15px;
}
.top {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
}
.lastbox {
  margin-bottom: 50px;
}
</style>